﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using Ninject.Modules;
using Quartz;
using Topshelf;
using Topshelf.Ninject;
using Topshelf.Quartz;
using Topshelf.Quartz.Ninject;
using Dln.Handler;
using Dln.Model;
using Dln.Common;
using Dln.Common.Helper;

namespace AutoRun
{
    class Program
    {
        static void Main()
        {
            log4net.Config.XmlConfigurator.Configure();

            HostFactory.Run(c =>
            {
                c.UseNinject(new SampleModule());
                c.SetDescription("AutoRun定时执行");
                c.SetServiceName("AutoRun");

                c.Service<SampleService>(s =>
                {
                    s.ConstructUsingNinject();

                    s.WhenStarted((service, control) => service.Start());
                    s.WhenStopped((service, control) => service.Stop());

                    s.UseQuartzNinject();


                    #region  发送统计通知 每天上午730点发送
                    s.ScheduleQuartzJob(q =>
                        q.WithJob(() => JobBuilder.Create<SampleJob>().Build())
                            .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 30 07 * * ? * ")//  "0 30 07 * * ? * ")
                            .StartNow().Build())
                            );
                    #endregion

                    #region  同步webservice 数据
                    s.ScheduleQuartzJob(q =>
                        q.WithJob(() => JobBuilder.Create<SearchWebserviceData>().Build())
                            .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 0 2 * * ?")//  "0 0 2 * * ?")
                            .StartNow().Build())
                            );
                    #endregion

                    #region  发送问题通知，每隔一小时0 0 0/1 * * ? 
                    s.ScheduleQuartzJob(q =>
                        q.WithJob(() => JobBuilder.Create<SendProblem>().Build())
                            .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 0 0/1 * * ? ")//  "0/20 * * * * ? ")
                            .StartNow().Build())
                            );
                    #endregion

                    #region  发送已接单，未完成问题通知，每天8、17点通知
                    s.ScheduleQuartzJob(q =>
                        q.WithJob(() => JobBuilder.Create<SendproblemComplete>().Build())
                            .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 0 8 * * ?")//  "0 0 8 * * ?")
                            .StartNow().Build())
                            );

                    s.ScheduleQuartzJob(q =>
                        q.WithJob(() => JobBuilder.Create<SendproblemComplete>().Build())
                            .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 0 17 * * ?")//  "0 0 17 * * ? ")
                            .StartNow().Build())
                            );
                    #endregion

                    #region  发送关闭问题通知，每隔一小时0 0 0/1 * * ? 
                    s.ScheduleQuartzJob(q =>
                        q.WithJob(() => JobBuilder.Create<CloseProblem>().Build())
                            .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 0 0/1 * * ? ")//  "0/20 * * * * ? ")
                            .StartNow().Build())
                            );
                    #endregion


                    //#region  信息科值日
                    //s.ScheduleQuartzJob(q =>
                    //    q.WithJob(() => JobBuilder.Create<DutyAction>().Build())
                    //        .AddTrigger(() => TriggerBuilder.Create().WithCronSchedule("0 00 07 * * ? * ")//  "0/20 * * * * ? ")
                    //        .StartNow().Build())
                    //        );
                    //#endregion

                });
            });

        }
    }

    public class SampleJob : IJob
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void Execute(IJobExecutionContext context)
        {
            log4net.Config.XmlConfigurator.Configure();
            Log.InfoFormat("1The current time is: {0}", DateTime.Now);
            var uptModel = SqlHelper.Context.From<WeChatUsers>().Where(d => d.ISSendWeChat1 == 1).ToList();
            if (uptModel != null && uptModel.Count > 0)
            {
                foreach (var item in uptModel)
                {
                    Log.InfoFormat(item.Openid);
                    MessageHelper.OrderPayNotice(item.Openid,
                                    "患者统计",
                                   DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                   "1",
                                   "点击通知查看详细，如有任何疑问直接回复公众号。",
                                    "http://www.hadln.cn/Page/AutoUpdate/WeChatStatics.aspx?NowDateTime=" + DateTime.Now.Date
                                );
                }
            }
            Log.InfoFormat("结束");
        }
    }

    public class SampleService
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public bool Start()
        {
            XmlConfigurator.Configure();
            Log.WarnFormat("The current time is: {0}", DateTime.Now);
            Console.WriteLine("XS.AutoRun Service Started...");
            return true;
        }

        public bool Stop()
        {
            Console.WriteLine("XS.AutoRun Service Stoped...");
            return true;
        }
    }

    public class SampleModule : NinjectModule
    {
        public override void Load()
        {

        }
    }
}
